<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>list</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

</head>
<body>
<div class="container">
    <input class="btn btn-primary" type="button" value="查询所有" @click="find()"><br>
    <table class="table ">
        <tr>
            <th>id</th>
            <th>名字</th>
            <th>工作</th>
            <th>工资</th>
            <th colspan="2">操作</th>
        </tr>

        <tr v-for="emp in arr">
            <td>{{emp.id}}</td>
            <td><a :href="'/update.html?id='+emp.id">{{emp.name}}</a></td>
            <td>{{emp.job}}</td>
            <td>{{emp.sal}}</td>
            <td><a data-toggle="modal" data-target="#myModal" @click="getemp(emp)">修改</a></td>
            <td><a @click="deleteemp(emp.id)">删除</a></td>
        </tr>
    </table>

    <!-- 按钮触发模态框 -->
    <!--<button class="btn btn-primary btn-lg" data-target="#myModal">
        开始演示模态框
    </button>-->
    <!-- 模态框（Modal） -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">修改</h4>
                </div>
                <div class="modal-body">
                    <form role="form" id="ddd" class="form-horizontal">
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">ID</label>
                            <div class="col-sm-10">
                                <input type="text" readonly class="form-control" :value="e.id" name="id">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">名字</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" v-model="e.name" name="name">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">job</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="job" v-model="e.job" >
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">工资</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="sal" v-model="e.sal" >
                            </div>
                        </div>

                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" @click="update()">提交更改</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
</div>


<!--引入axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!--引入vue框架-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
<!--sweetalert-->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>

    let v = new Vue({
        el:"div",
        data:{
            arr: [],
            e: {
                id:"",
                name:"",
                job:"",
                sal:""
            },
            info:""
        },
        methods:{
            find:function () {
                axios.post("/selectall").then(function (response) {
                    //服务器返回的内容实际上是Json格式的字符串，axios框架内部会将此字符串
                    // 转成数组装对象的格式│
                    v.arr=response.data;
                })
            },
            getemp:function (emp) {
                v.info="";
                v.e.id=emp.id;
                v.e.name=emp.name;
                v.e.job=emp.job;
                v.e.sal=emp.sal;
                // v.u=user;
            },
            update:function () {
                let data = new FormData();
                data.append("id",v.e.id);
                data.append("name",v.e.name);
                data.append("job",v.e.job);
                data.append("sal",v.e.sal);
                // console.log(data);
                axios.post("/update",data).then(function (response) {
                    //返回1修改成功,返回0修改失败
                    if (response.data == 1){
                        swal({
                            title: "修改成功!",
                            text: "3秒钟后关闭!",
                            icon: "success",
                            button: "关闭",
                            timer: 3000,
                        });
                        $('#myModal').modal('hide');
                        // swal("This modal will disappear soon!", {
                        //     buttons: false,
                        //     timer: 3000,
                        // });
                        v.info="修改成功!"
                    }else {
                        swal({
                            title: "修改失败!",
                            text: "3秒钟后关闭!",
                            icon: "error",
                            button: "关闭",
                            timer: 3000,
                        });
                        v.info="修改失败!"
                    }

                    v.find();
                })
            },
            deleteemp:function (id) {
                var r = confirm("是否删除?");
                if (r == true) {
                    let data = new FormData();
                    data.append("id",id);
                    axios.post("/delete",data).then(function (response) {
                        //返回1删除成功,返回0删除失败
                        if (response.data == 1){
                            alert("删除成功!")
                        }else {
                            alert("删除失败!")
                        }
                        v.find();
                    })
                }
            }
        },
        created:function () {//创建时执行
            axios.post("/selectall").then(function (response) {
                //服务器返回的内容实际上是Json格式的字符串，axios框架内部会将此字符串
                // 转成数组装对象的格式│
                v.arr = response.data;
            })
        }
    })
</script>
</body>
</html>